<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'ispartner'=>1);
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$fields=array('id','mid','partnerlevel','partnertime','createtime','partnerstatus');
    $orderby='partnertime desc';
	$total='';
	$list=pdo_getslice('jy_robshop_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname','mobile','realname'));
		$v['avatar']=$member['avatar'];
		$v['nickname']=$member['nickname'];
		$v['mobile']=$member['mobile'];
		$v['realname']=$member['realname'];
		$v['createtime']=date('Y/m/d H:m:s',$v['createtime']);
		$v['partnertime']=$v['partnertime']?date('Y/m/d H:m:s',$v['partnertime']):'';
		$v['partnerlevel']=$v['partnerlevel']?pdo_getcolumn('jy_robshop_gu_level',array('id'=>$v['partnerlevel']),'levelname'):'默认等级';
		$v['down']=pdo_count('core_members',array('fromuser'=>$v['id']));
		$credit_logs=pdo_getall('core_credit_log',array('is_add'=>1,'remark'=>'股东分红','plugin'=>'jy_robshop_gu','type'=>1,'mid'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']));	
		foreach($credit_logs as $c){
			if($c['id'] && $c['fee']>0){
				pdo_update('core_credit_log',array('type'=>2),array('id'=>$c['id'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('core_members',array('credit2 +='=>$c['fee'],'credit1 -='=>$c['fee']),array('merchid'=>0,'id'=>$c['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	$return['status']=array('0'=>'待审核','1'=>'已审核','已拒绝');
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'股东管理'),
			
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),								
			array('name'=>'realname','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'partnerlevel','title'=>'股东等级','type'=>'string'),								
			array('name'=>'partnertime','title'=>'成为股东时间','type'=>'string'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'string'),								
			array('name'=>'down','title'=>'下线数','type'=>'string'),
			array('name'=>'partnerstatus','title'=>'状态','type'=>'radio','options'=>'status'),			
			
		),
		'action'=>array(
			array('op'=>'check','title'=>'审核','type'=>'modal'),
			
			),
		),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'昵称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		pdo_update('jy_robshop_member',array('mid'=>$post['mid'],'partnerlevel'=>$post['partnerlevel']),array('id'=>$id));
		exi('修改股东成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_robshop_member',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'股东管理'),
		),
		'edit'=>array(
			array('type'=>'choose','name'=>'mid','title'=>'绑定会员','action'=>'core/member.get_member/','help'=>'绑定的系统会员'),
			array('name'=>'partnerlevel','title'=>'股东等级','type'=>'select','options'=>'levels','option_title'=>'levelname'),	
		)
	);
	$return['levels']=pdo_getall('jy_robshop_gu_level',array('uniacid'=>$_SESSION['uniacid']),array('id','levelname'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check'){
	$id=intval($_RQ['id']);
	$return['status']=array('0'=>'待审核','1'=>'已审核','拒绝');
	$return['item']=pdo_get('jy_robshop_member',array('id'=>$id));
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
		
			array('name'=>'partnerstatus','title'=>'状态','type'=>'radio','options'=>'status'),
			)
			);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$post=get_postdata();
	pdo_update('jy_robshop_member',array('partnertime'=>time(),'partnerstatus'=>$post['partnerstatus']),array('id'=>$post['id']));
	exi('审核成功','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_robshop_member',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_robshop_member',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>